<?php

namespace App\Http\Middleware;

use App\Repositories\AccessLogRepository;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class LogAccess
{
    public function handle(Request $request, Closure $next): Response
    {
        // 如果是后台请求则不记录
        if ($request->is('admin/*')) {
            return $next($request);
        }
        app(AccessLogRepository::class)->log([
            'ip' => $request->ip(),
            'url' => $request->fullUrl(),
            'method' => $request->method(),
            'agent' => $request->header('user-agent'),
            'referer' => $request->header('referer'),
        ]);
        return $next($request);
    }
}
